﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace IronWinter
{
    public class Bullet
    {
        public Vector2 position;
        public AnimatedSprite sprite;
        public Unit source, uTarget;
        public Building bTarget;
        private Vector2 direction;

        public static Texture2D[] bulletTextures;

        public Boolean killFlag = false;

        public Bullet(Unit attacker, Unit target, Color newColor)
        {
            source = attacker;
            position = attacker.Center;
            uTarget = target;
            direction = new Vector2();
            sprite = new AnimatedSprite(bulletTextures[0], 0, 0, 8, 8, 1);
            sprite.tint = newColor;
        }

        public Bullet(Unit attacker, Building target, Color newColor)
        {
            source = attacker;
            position = attacker.Center;
            bTarget = target;
            direction = new Vector2();
            sprite = new AnimatedSprite(bulletTextures[0], 0, 0, 8, 8, 1);
            sprite.tint = newColor;
        }


        public void Update()
        {
            Vector2 target = uTarget == null ? bTarget.Center : uTarget.Center;

            direction = target - position;
            direction.Normalize();

            position.X += direction.X * 10;
            position.Y += direction.Y * 10;

            if (uTarget != null && uTarget.Bounds.Contains(new Point((int)position.X, (int)position.Y)))
            {
                killFlag = true;
                uTarget.takeDamage(source);
            }
            else if (bTarget != null && bTarget.hitBox.Contains(new Point((int)position.X, (int)position.Y)))
            {
                killFlag = true;
                bTarget.takeDamage(source);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            sprite.Draw(spriteBatch, position, 1);
        }
    }
}
